package com.har.modules.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.har.common.utils.PageUtils;
import com.har.common.utils.Query;
import com.har.modules.web.dao.BaseProducDao;
import com.har.modules.web.entity.*;
import com.har.modules.web.service.BaseProducService;
import com.har.modules.web.service.FinanceProducService;
import com.har.modules.web.service.FinancialService;
import com.har.modules.web.service.InfoServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("baseProducService")
public class BaseProducServiceImpl extends ServiceImpl<BaseProducDao, BaseProducEntity> implements BaseProducService {

    @Autowired
    private FinanceProducService financeProducService;
    @Autowired
    private FinancialService financialService;
    @Autowired
    private InfoServiceService infoServiceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<BaseProducEntity> page = this.selectPage(
                new Query<BaseProducEntity>(params).getPage(),
                new EntityWrapper<BaseProducEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public BaseAndFinancialEntity queryFinancial(Integer type) {
        EntityWrapper<BaseProducEntity> ew = new EntityWrapper<>();
        BaseProducEntity baseProducEntity = null;
        EntityWrapper<FinancialEntity> ewf = new EntityWrapper<>();
        BaseAndFinancialEntity baseAndFinancialEntity = new BaseAndFinancialEntity();
        if(null!=type){
            baseProducEntity = this.selectOne(ew.eq("product_type", type));
            List<FinancialEntity> list = financialService.selectList(
                    ewf.eq("product_type", type).eq("status", 1).orderBy("publish_time DESC"));
            if (null != baseProducEntity && !list.isEmpty()) {
                baseAndFinancialEntity.setFinancialEntityList(list);
                baseAndFinancialEntity.setImage(baseProducEntity.getImage());
                baseAndFinancialEntity.setSlogan(baseProducEntity.getSlogan());
                baseAndFinancialEntity.setProductTypeName(baseProducEntity.getProductTypeName());
            }
        }
        return baseAndFinancialEntity;
    }

    @Override
    public BaseAndSMartStoreEntity querysmartstore(Integer type) {
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        BaseProducEntity baseProducEntity = null;
        BaseAndSMartStoreEntity baseEntity=new BaseAndSMartStoreEntity();
        //智慧门店只需要主表数据
        baseProducEntity=this.selectOne(ew.eq("product_type",type).orderBy("update_time DESC"));
        if(null!=baseProducEntity){
            baseEntity.setImage(baseProducEntity.getImage());
            baseEntity.setSlogan(baseProducEntity.getSlogan());
            baseEntity.setProductTypeName(baseProducEntity.getProductTypeName());
        }
        return baseEntity;
    }

    @Override
    public BaseProducEntity selectByType(Integer type) {
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        return  this.selectOne(ew.eq("product_type",type));
    }

    /**
     * 数据服务模块

     * @return
     */
    @Override
    public BaseAndInfoServiceEntity queryInfoService() {

        EntityWrapper<BaseProducEntity> ewf = new EntityWrapper<>();
        BaseAndInfoServiceEntity baseAndInfoServiceEntity = new BaseAndInfoServiceEntity();
        //通过类型查询查询主表数据
        BaseProducEntity baseProducEntity=this.selectOne(ewf.eq("product_type",2));
        if(null!=baseProducEntity) {
            String [] types=baseProducEntity.getType().split(",");
            List<InfoEntity> list=new ArrayList<>();
            //通过遍历主表的所属类型去查询副表的数据服务数据
            for (int i=0;i<types.length;i++){
                EntityWrapper<InfoServiceEntity> ew = new EntityWrapper<>();
                ew.eq("status",1).orderBy("update_time DESC");
                InfoEntity infoEntity=new InfoEntity();
                String type=types[i];
                //按照类型查询数据服务
                List<InfoServiceEntity> infoServiceEntityList=infoServiceService.selectList(
                        ew.eq("type",type));
                //封装数据
                infoEntity.setList(infoServiceEntityList);
                infoEntity.setType(type);
                list.add(infoEntity);
                baseAndInfoServiceEntity.setList(list);
            }

            baseAndInfoServiceEntity.setImage(baseProducEntity.getImage());
            baseAndInfoServiceEntity.setSlogan(baseProducEntity.getSlogan());
        }
        return baseAndInfoServiceEntity;

    }

    /**
     * 贷款模块
     * @return
     */
    @Override
    public BaseEntity queryLoan(){
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        BaseProducEntity baseProducEntity = null;
        BaseEntity baseEntity=new BaseEntity();
        //查询主表的宣传语以及栏目图片
        baseProducEntity=this.selectOne(ew.eq("product_type",1));
        if (null!=baseProducEntity){
            String [] types=baseProducEntity.getType().split(",");
            List<BaseLoanEntity> baseLoanEntityList=new ArrayList<>();
            for (int i=0;i<types.length;i++){
                BaseLoanEntity baseLoanEntity=new BaseLoanEntity();
                EntityWrapper<FinanceProducEntity> ew2=new EntityWrapper<>();
                InfoEntity infoEntity=new InfoEntity();
                String type=types[i];
                //按照类型查询数据
                List<FinanceProducEntity> financeProducEntityList=financeProducService.selectList(
                        ew2.eq("product_type",1).eq("status",1)
                                .eq("type",type).orderBy("update_time DESC"));
                //封装数据
                baseLoanEntity.setList(financeProducEntityList);
                baseLoanEntity.setType(type);
                baseLoanEntityList.add(baseLoanEntity);
                baseEntity.setBaseLoanList(baseLoanEntityList);
            }

            baseEntity.setImage(baseProducEntity.getImage());
            baseEntity.setSlogan(baseProducEntity.getSlogan());
        }
        return baseEntity;
    }

    /**
     * 保险模块
     * @return
     */
    @Override
    public BaseEntity queryInsurance() {
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        BaseProducEntity baseProducEntity = null;
        BaseEntity baseEntity=new BaseEntity();
        //查询主表的宣传语以及栏目图片
        baseProducEntity=this.selectOne(ew.eq("product_type",4));
        //封装数据
        if (null!=baseProducEntity){
            String [] types=baseProducEntity.getType().split(",");
            List<BaseLoanEntity> baseLoanEntityList=new ArrayList<>();
            for (int i=0;i<types.length;i++){
                BaseLoanEntity baseLoanEntity=new BaseLoanEntity();
                EntityWrapper<FinanceProducEntity> ew2=new EntityWrapper<>();
                String type=types[i];
                //按照类型查询启用的数据
                List<FinanceProducEntity> financeProducEntityList=financeProducService.selectList(
                        ew2.eq("product_type",4).eq("status",1)
                                .eq("type",type).orderBy("update_time DESC"));
                baseLoanEntity.setList(financeProducEntityList);
                baseLoanEntity.setType(type);
                baseLoanEntityList.add(baseLoanEntity);
                baseEntity.setBaseLoanList(baseLoanEntityList);
            }

            baseEntity.setImage(baseProducEntity.getImage());
            baseEntity.setSlogan(baseProducEntity.getSlogan());
        }
        return baseEntity;

    }

    @Override
    public List<BaseLoanEntity> queryLoanList() {
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        //查询主表的宣传语以及栏目图片
        BaseProducEntity baseProducEntity=this.selectOne(ew.eq("product_type",4));
        //封装数据
        List<BaseLoanEntity> baseLoanEntityList = new ArrayList<>();
        if (null!=baseProducEntity) {
            String[] types = baseProducEntity.getType().split(",");

            for (int i = 0; i < types.length; i++) {
                BaseLoanEntity baseLoanEntity = new BaseLoanEntity();
                EntityWrapper<FinanceProducEntity> ew2 = new EntityWrapper<>();
                String type = types[i];
                //按照类型查询启用的数据
                List<FinanceProducEntity> financeProducEntityList = financeProducService.selectList(
                        ew2.eq("product_type", 4).eq("status", 1)
                                .eq("type", type).orderBy("update_time DESC"));
                baseLoanEntity.setList(financeProducEntityList);
                baseLoanEntity.setType(type);
                baseLoanEntityList.add(baseLoanEntity);

            }

        }
            return baseLoanEntityList;
    }

    @Override
    public List<InfoEntity> queryInfoList() {
        EntityWrapper<BaseProducEntity> ew1 = new EntityWrapper<>();
        //通过类型查询查询主表数据
        BaseProducEntity baseProducEntity=this.selectOne(ew1.eq("product_type",2));
        List<InfoEntity> list=new ArrayList<>();
        if(null!=baseProducEntity) {
            String [] types=baseProducEntity.getType().split(",");
            //通过遍历主表的所属类型去查询副表的数据服务数据
            for (int i=0;i<types.length;i++){
                EntityWrapper<InfoServiceEntity> ew = new EntityWrapper<>();
                ew.eq("status",1).orderBy("update_time DESC");
                InfoEntity infoEntity=new InfoEntity();
                String type=types[i];
                //按照类型查询数据服务
                List<InfoServiceEntity> infoServiceEntityList=infoServiceService.selectList(
                        ew.eq("type",type));
                //封装数据
                infoEntity.setList(infoServiceEntityList);
                infoEntity.setType(type);
                list.add(infoEntity);

            }
        }
        return list;
    }

    @Override
    public List<BaseLoanEntity> queryInsuranceList() {
        EntityWrapper<BaseProducEntity> ew=new EntityWrapper<>();
        //查询主表的宣传语以及栏目图片
        BaseProducEntity  baseProducEntity=this.selectOne(ew.eq("product_type",4));
        List<BaseLoanEntity> baseLoanEntityList=new ArrayList<>();
        //封装数据
        if (null!=baseProducEntity){
            String [] types=baseProducEntity.getType().split(",");

            for (int i=0;i<types.length;i++){
                BaseLoanEntity baseLoanEntity=new BaseLoanEntity();
                EntityWrapper<FinanceProducEntity> ew2=new EntityWrapper<>();
                String type=types[i];
                //按照类型查询启用的数据
                List<FinanceProducEntity> financeProducEntityList=financeProducService.selectList(
                        ew2.eq("product_type",4).eq("status",1)
                                .eq("type",type).orderBy("update_time DESC"));
                baseLoanEntity.setList(financeProducEntityList);
                baseLoanEntity.setType(type);
                baseLoanEntityList.add(baseLoanEntity);

            }
        }
        return baseLoanEntityList;
    }

}
